const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    userinfo: wx.getStorageSync('userinfo'),
    // 个人信息
    infoArray: {
      alias: '',
      school: '',
      city: '',
      province: '',
      address: '',
    },
    region: ['浙江省', '杭州市', '江干区'],
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
    await wx.User.getUserInfo()
    // 获取从我的页面传过来的对象
    this.setData({
      userinfo: wx.getStorageSync('userinfo'),
      infoArray: wx.getStorageSync('info'),
    })
  },
  RegionChange: function (e) {
    this.setData({
      region: e.detail.value
    })
  },
  aliasIn: function (e) {
    this.data.infoArray.alias = e.detail.value
  },
  schoolIn: function (e) {
    this.data.infoArray.school = e.detail.value
  },
  // 提交修改信息
  onSubmit: async function () {
    this.data.infoArray.province =  this.data.region[0]
    this.data.infoArray.city =  this.data.region[1]
    this.data.infoArray.address =  this.data.region[2]
		wx.setStorageSync("userinfo", this.data.infoArray);
    new Promise(async (resolve, reject) => {
      await wx.User.updateUserInfo({
        data: {
          uid: this.data.userinfo.uid,
          alias: this.data.infoArray.alias,
          province: this.data.region[0],
          city: this.data.region[1],
          address: this.data.region[2],
          school: this.data.infoArray.school,
        }
      })
      resolve(wx.getStorageSync('userinfo'))
    }).then(res => {
      wx.navigateBack({
        delta: 1,
      })
    })


  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: async function () {
    await wx.User.getUserInfo()
    this.setData({
      userinfo: wx.getStorageSync('userinfo'),
      infoArray: wx.getStorageSync('userinfo'),
      // infoArray: JSON.parse(options.info)
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})